﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using System.Xml.Serialization;

namespace RRRSRoguelikeData
{
    /// <summary>
    /// A class for reading text files.
    /// </summary>
    public static class Files
    {
        /// <summary>
        /// A method that returns a string containing the contents of the file.
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns>A string containing the contents of the file. </returns>
        public static string TxtFileToString(string filePath)
        {
            string text;
            //needs to be more robust.  Error handling etc.
            try
            {
                StreamReader streamReader = new StreamReader(filePath);
                text = streamReader.ReadToEnd();
                streamReader.Close();
            }
            catch
            {
                text = System.IO.Path.GetFileName(filePath) + " not available.";
            }

            return text;
        }
        
        
                public static TileData ReadFromXML(string filepath) 
        {
            TileData tileData = new TileData();

            try
            {
                XmlSerializer TileSerializer = new XmlSerializer(typeof(TileData));

                FileStream TileFileStream = new FileStream(filepath, FileMode.Open, FileAccess.Read);

                tileData = (TileData)TileSerializer.Deserialize(TileFileStream);
            }
            catch 
            {
                tileData.ForeColor = "Red";
                tileData.BackColor = "White";
                tileData.ImageCharacter = "?";
                tileData.Name = "missing file";
                tileData.Description = "missing file: " + filepath;
                tileData.Walkable = true;

            }
            return tileData;
            
        }
        
        public static DungeonData ReadDataFromXML(string filepath) 
        {
            DungeonData Data = new DungeonData();

            //try
            {
            	XmlDocument XmlDoc = new XmlDocument();
				
                XmlDoc.Load(filepath);
                
                XmlNodeList Name = XmlDoc.GetElementsByTagName("Name");
                XmlNodeList Desription = XmlDoc.GetElementsByTagName("Description");
                XmlNodeList Messages = XmlDoc.GetElementsByTagName("Message");
                XmlNodeList Players = XmlDoc.GetElementsByTagName("Player");
                XmlNodeList Creatures = XmlDoc.GetElementsByTagName("Creature");
                XmlNodeList Props = XmlDoc.GetElementsByTagName("Prop");
                XmlNodeList Tiles = XmlDoc.GetElementsByTagName("Tile");
                XmlNodeList Levels = XmlDoc.GetElementsByTagName("Level");
                
                Data.Name = Name[0].InnerText;
                Data.Description = Desription[0].InnerText;
                
                //this should load the txt from file
                foreach (XmlNode n in Messages)
                	Data.Texts.Add(Path.GetFileNameWithoutExtension(n.InnerText),
                		Files.TxtFileToString(Path.Combine(Path.GetDirectoryName(filepath),@n.InnerText)));
                
                foreach (XmlNode n in Players)
                	Data.Players.Add(Path.GetFileNameWithoutExtension(n.InnerText),
                	    Files.ReadFromXML(Path.Combine(Path.GetDirectoryName(filepath),n.InnerText)));
                
                foreach (XmlNode n in Creatures)
                	Data.Creatures.Add(Path.GetFileNameWithoutExtension(n.InnerText),
                		Files.ReadFromXML(Path.Combine(Path.GetDirectoryName(filepath),n.InnerText)));
                
                foreach (XmlNode n in Props)
                	Data.Props.Add(Path.GetFileNameWithoutExtension(n.InnerText),
                		Files.ReadFromXML(Path.Combine(Path.GetDirectoryName(filepath),n.InnerText)));
                
                foreach (XmlNode n in Tiles)
                	Data.Tiles.Add(Path.GetFileNameWithoutExtension(n.InnerText),
                		Files.ReadFromXML(Path.Combine(Path.GetDirectoryName(filepath),n.InnerText)));
                
                foreach (XmlNode n in Levels)
                	Data.Levels.Add(n.InnerText);
                
                
            }
            //catch 
            {
                

            }
            return Data;
            
        }        
                
        /// <summary>
		/// 
		/// </summary>
		/// <param name="sourcePath"></param>
		/// <param name="targetPath"></param>
		/// <param name="overwrite"></param>
        public static void CopyFolder(string sourcePath, string targetPath, bool overwrite)
        {
            string fileName;
            string destFile;
            if (System.IO.Directory.Exists(sourcePath))
            {
                string[] files = System.IO.Directory.GetFiles(sourcePath);

                // Copy the files and overwrite destination files if they already exist. 
                foreach (string s in files)
                {
                    // Use static Path methods to extract only the file name from the path.
                    fileName = System.IO.Path.GetFileName(s);
                    destFile = System.IO.Path.Combine(targetPath, fileName);

                    //Create targetPath
                    Directory.CreateDirectory(targetPath);
                    
                    if(overwrite || !System.IO.File.Exists(destFile))
                    {
                        System.IO.File.Copy(s, destFile, overwrite);
                    }

                }
                string[] folders = Directory.GetDirectories(sourcePath);
                foreach (string s in folders)
                {
                    string name = Path.GetFileName(s);
                    string dest = Path.Combine(targetPath, name);
                    CopyFolder(s, dest,false);
                }
            }
            else
            {
                Console.WriteLine("Source path does not exist!");
                Console.ReadKey();
            }
        }
        
    }
}
